package com.logic.landseaserver.models.rerent.ws;

import java.util.HashMap;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.models.rerent.service.IReRentService;
import com.logic.landseaserver.ws.request.CreateOffLineContractsReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by Junny on 2017/11/23.
 * M3
 * 续租
 */
@Path("/reRent")
@Api(value="reRent")
public class ReRentRest {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IReRentService reRentService;

    @POST
    @Path("/createReRent")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新增续租合同", position = 1)
    public Response createReRent(@ApiParam(value = "param", required = true) CreateOffLineContractsReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(reRentService.createReRent(req));
            map.setResult(CommonResult.SUCCESS, "createReRent success");
        } catch (LandseaException e) {
            logger.error("createReRent LandseaException error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getErrorDesc());
        } catch (BusinessException e) {
            logger.error("createReRent BusinessException error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createReRent error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/createOverList/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新增续租结转单")
    public Response createOverList(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            reRentService.createOverList(contractId);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e) {
            logger.error("ReRent || createOverList error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("ReRent || createOverList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/synOverList/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "同步A合同结转单至B合同首期款，contractId->B合同")
    public Response synOverList(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            reRentService.synOverList(contractId);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e) {
            logger.error("ReRent || createOverList error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("ReRent || createOverList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/createPreBill/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "生成续租预结算单")
    public Response createPreBill(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            reRentService.createPreBill(contractId);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e) {
            logger.error("reRent || createPreBill error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("reRent || createPreBill error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @DELETE
    @Path("/cancelReRent/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "取消续租 contractId->A合同编号")
    public Response cancelReRent(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId){
        CommonResultMap map = new CommonResultMap();
        try {
            HashMap<String,Object> result = new HashMap<>();
            result.put("result", reRentService.cancelReRent(contractId));
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            logger.error("rerent || cancelReRent error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("rerent || cancelReRent error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/getReRentContract/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取续租合同信息，contractId->A合同")
    public Response getReRentContract(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(reRentService.queryReRentContract(contractId));
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("ReRent || getReRentContract error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/confirmRerent/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "确认续租->提交续租确认")
    public Response confirmRerent(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            reRentService.confirmRerent(contractId);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("offRent || confirmRerent error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

}
